package splug.cjk;

import java.io.BufferedOutputStream;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStreamWriter;
import java.net.Socket;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;


public class Login extends Thread{

	private String id;
	private String pw;
	private OutputStreamWriter osw;


	Login(String id, String pw, OutputStreamWriter osw){
		this.id = id;
		this.pw = pw;
		this.osw = osw;
	}


	public void run(){

		try {
			Class.forName("com.mysql.jdbc.Driver");
			Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/carpool?useUnicode=true&characterEncoding=euckr","root","apmsetup");
			Statement stmt = conn.createStatement();

			String query = "SELECT passwd FROM member WHERE id = '" + id + "'";

			System.out.println(query);

			stmt.execute("SET CHARACTER SET euckr");
			stmt.execute("set names euckr");

			ResultSet result = stmt.executeQuery(query);

			String db_pw = "";

			while(result.next()){
				db_pw = result.getString("passwd");
			}

			if(db_pw.equals(pw)){

				System.out.println(id + "Login OK");

				try {
					osw.write('1');
					osw.flush();

				} catch (IOException e) {
					e.printStackTrace();
				}

			}
			else{

				System.out.println(id + "Login fail!");

				try {
					osw.write('0');
					osw.flush();
					osw.close();

				} catch (IOException e) {
					e.printStackTrace();
				}

			}

		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
}
